package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.game2048.lib.bean.G2048OpenInfo;
import com.stark.game2048.lib.bean.GameStateInfo;
import com.stark.game2048.lib.manager.GameStateManager;
import com.stark.game2048.lib.util.G2048PrefUtil;
import com.stark.game2048.lib.view.CellEntity;
import com.stark.game2048.lib.view.GameView;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityGameBinding;
import flc.ast.utils.Content;

public class GameActivity extends BaseAc<ActivityGameBinding> {

    private int mHistoryMaxScore = 0;//历史最高得分
    private int mCompleteCount = 0;//完成次数
    private GameStateManager mGameStateManager;
    private Dialog myGamePauseDialog;

    @Override
    protected int onCreate() {
        return R.layout.activity_game;
    }

    @Override
    protected void initView() {
        mDataBinding.ivGamerPause.setOnClickListener(this);

        mDataBinding.ivGamerPause.setOnClickListener(this);

        //暂停弹框
        gamePauseDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivGamerPause://暂停
                myGamePauseDialog.show();
                break;
            //暂停弹框
            case R.id.ivDialogGamePauseRestart://重新开始
                mDataBinding.gameView.resetGame();
                myGamePauseDialog.dismiss();
                break;
            case R.id.ivDialogGamePauseHome://返回首页
                startActivity(HomeActivity.class);
                break;
            case R.id.ivDialogGamePauseContinue://继续游戏
                myGamePauseDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {
        initGame();
    }

    //初始化游戏
    private void initGame() {
        mGameStateManager = new GameStateManager();
        //历史最高得分
        mHistoryMaxScore = G2048PrefUtil.getHistoryMaxScore();
        //完成次数
        mCompleteCount = G2048PrefUtil.getCompleteGameNumber();
        //游戏容器监听
//        mDataBinding.gameView.setGoalNumber(passScore);//设置通关条件值
        mDataBinding.gameView.setListener(new GameView.IListener() {
            @Override
            public void onBeforeCellChanged(int score, List<CellEntity> cellEntityList) {
                //监听步骤
                GameStateInfo stateInfo = new GameStateInfo(score, cellEntityList);
                mGameStateManager.pushGameStateInfo(stateInfo);
            }

            @Override
            public void onRecordScore(int score) {
                //得分
                updateCurScoreView(score);
                if (score > mHistoryMaxScore) {
                    mHistoryMaxScore = score;
                    G2048PrefUtil.saveHistoryMaxScore(mHistoryMaxScore);
                    updateHistoryMaxScoreView(mHistoryMaxScore);
                }
            }

            @Override
            public void onGetGoal() {
                //达成目标
                mCompleteCount++;
                G2048PrefUtil.saveCompleteGameNumber(mCompleteCount);
                updateCompleteCountView(mCompleteCount);
                //您已通关
                ToastUtils.showShort(getString(R.string.next_level));
            }

            @Override
            public void onGameOver() {
                //游戏结束后，清除所有保存的局面信息
                mGameStateManager.clear();
                //游戏结束
                ToastUtils.showShort(getString(R.string.game_over));
            }
        });
        int curScore = 0;
        //开局模式
//        GameStateInfo stateInfo = mGameStateManager.popGameStateInfo();
//
//        if (stateInfo != null) {
//            curScore = stateInfo.getScore();
//            mDataBinding.gameView.resumeGame(stateInfo.getCellList(), curScore);
//        }
        //更新得分数据
        updateCurScoreView(curScore);
        updateHistoryMaxScoreView(mHistoryMaxScore);
        updateCompleteCountView(mCompleteCount);
    }

    //更新当前得分
    private void updateCurScoreView(int score) {
        mDataBinding.tvGameScore.setText(String.valueOf(score));
    }

    //更新历史最高得分
    private void updateHistoryMaxScoreView(int score) {
        mDataBinding.tvGameMaxScore.setText(String.valueOf(score));
    }

    //更新完成次数
    private void updateCompleteCountView(int count) {
        mDataBinding.tvGameFrequency.setText(String.valueOf(count));
    }

    //暂停弹框
    private void gamePauseDialog() {
        myGamePauseDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_game_pause, null);
        myGamePauseDialog.setContentView(inflate);
        myGamePauseDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myGamePauseDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        lp.height = dm.heightPixels;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView restart = inflate.findViewById(R.id.ivDialogGamePauseRestart);
        ImageView home = inflate.findViewById(R.id.ivDialogGamePauseHome);
        ImageView continueGame = inflate.findViewById(R.id.ivDialogGamePauseContinue);
        restart.setOnClickListener(this);
        home.setOnClickListener(this);
        continueGame.setOnClickListener(this);
    }

}